﻿using System;

using System.Collections.Generic;

using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.Tools;



namespace HR60_PanelSoft.Model
{


    //GC: [g1.s1] 增加反射父设备功能


    /// <summary>
    /// 设备对象
		// find by uuid
    /// </summary>

    public class Device : RefModel
    {

        /// <summary>
        /// 昵称
        /// </summary>

        public string nickName = "";



        /// <summary>
        /// 设备类型
        /// </summary>

        public DeviceType type = null;



        /// <summary>
        /// 设备品牌
        /// </summary>

        public DeviceBrand brand = null;



        /// <summary>
        /// 子设备列表
        /// </summary>

        public List<Device> childDevices = new List<Device>();



        /// <summary>
        /// 已连接的线路列表,ref
        /// </summary>

        public List<string> connectedPins = new List<string>();



        /// <summary>
        /// 已连接的线路列表, ref to object
        /// </summary>
        /// <returns>The connnected pins.</returns>

        public List<Pin> getConnnectedPins()
        {

            List<Pin> list = new List<Pin>();

            foreach (string uuid in connectedPins)
            {

                list.Add(Pin.find(uuid));

            }

            return list;

        }



        // find by uuid

        public static Device find(string uuid)
        {

            foreach (DeviceGroup group in Proj.currentProj.deviceGroups)
            {

                foreach (Device device in group.devices)
                {

                    if (device.uuid == uuid)

                        return device;

                    if (device.childDevices == null)

                        continue;

                    foreach (Device sub in device.childDevices)
                    {

                        if (sub.uuid == uuid)

                            return sub;

                    }

                }

            }

            return null;

        }





        /* [g1.s1] 获取父设备 */

        /// <summary>
        /// 根据子设备的UUID获取父设备与子设备对象
        /// </summary>
        /// <param name="subDevUuid">子设备的UUID</param>
        /// <param name="subDevice">子设备自身对象</param>
        /// <returns>有则返回父设备对象，否则返回null</returns>

        public static Device findParent(string subDevUuid, out Device subDevice)
        {

            foreach (DeviceGroup group in Proj.currentProj.deviceGroups)
            {

                foreach (Device device in group.devices)
                {

                    if (device.childDevices.Count < 1)
                    {

                        if (device.uuid == subDevUuid)
                        {

                            subDevice = device;

                            return null;

                        }

                        continue;

                    }

                    Device parent = device;

                    foreach (Device sub in device.childDevices)
                    {

                        if (sub.uuid == subDevUuid)
                        {

                            subDevice = sub;

                            return parent;

                        }

                    }

                }

            }

            return subDevice = null;

        }

        public static Device findParent(Device subDev)
        {
             if (subDev == null)
                return null;

            foreach (DeviceGroup group in Proj.currentProj.deviceGroups)
            {

                foreach (Device device in group.devices)
                {

                    if (device.childDevices.Count < 1)
                    {

                        if (device.uuid == subDev.uuid)

                            return null;

                        continue;

                    }

                    Device parent = device;
                    foreach (Device sub in device.childDevices)
                    {

                        if (sub.uuid == subDev.uuid)
                        {

                            return parent;

                        }

                    }

                }

            }

            return null;
        }




        public static DoubleReuslt<Device, int> findParentAndDoubleReuslt(Device subDev)
        {

            if (subDev == null)

                return null;

            foreach (DeviceGroup group in Proj.currentProj.deviceGroups)
            {

                foreach (Device device in group.devices)
                {

                    if (device.childDevices == null)
                    {

                        if (device.uuid == subDev.uuid)

                            return null;

                        continue;

                    }

                    Device parent = device;
                    int i = 0;
                    foreach (Device sub in device.childDevices)
                    {
                        i++;

                        if (sub.uuid == subDev.uuid)
                        {

                            return new DoubleReuslt<Device, int>(parent,i);

                        }

                    }

                }

            }

            return null;

        }

       

    }

}